using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using QLNT_DTO;
using System.Collections;

namespace QLNT_DAO
{
    public class TaiKhoan_dao
    {

        public int ThietLapTaiKhoan(string TenTK, string MatMaTK, string HoTenNV, int MaQuyen)
        {
            try
            {
                List<SqlParameter> listpara = new List<SqlParameter>();
                listpara.Add(new SqlParameter("@TenTaiKhoan", TenTK));
                listpara.Add(new SqlParameter("@MatMaTaiKhoan", MatMaTK));
                listpara.Add(new SqlParameter("HoTenNhanvien", HoTenNV));
                listpara.Add(new SqlParameter("@MaQuyen", MaQuyen));
                return MyExecute.ExecuteNoneQuery("sp_ThietLapTaiKhoan", listpara);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int ThayDoiTaiKhoan(string TenTK, string MatMaTK, string HoTenNV, int MaQuyen)
        {
            try
            {
                List<SqlParameter> listpara = new List<SqlParameter>();
                listpara.Add(new SqlParameter("@TenTaiKhoan", TenTK));
                listpara.Add(new SqlParameter("@MatMaTaiKhoan", MatMaTK));
                listpara.Add(new SqlParameter("HoTenNhanvien", HoTenNV));
                listpara.Add(new SqlParameter("@MaQuyen", MaQuyen));
                return MyExecute.ExecuteNoneQuery("sp_CapNhatTaiKhoan", listpara);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int XoaTaiKhoan(string TenTaiKhoan)
        {
            try
            {
                List<SqlParameter> listpara = new List<SqlParameter>();
                listpara.Add(new SqlParameter("@TenTaiKhoan", TenTaiKhoan));
                return MyExecute.ExecuteNoneQuery("sp_XoaTaiKhoan", listpara);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public TaiKhoan_dto[] SelectAll()
        {
            TaiKhoan_dto[] tk = new TaiKhoan_dto[100];
            DataTable dt = new DataTable();
            try
            {
                dt = MyExecute.ExecuteQuery("sp_LayDSTaiKhoan");
                int n = dt.Rows.Count;
                tk = new TaiKhoan_dto[n];
                for (int i = 0; i < n; i++)
                {
                    tk[i] = new TaiKhoan_dto();
                    tk[i].TenTaiKhoan = dt.Rows[i]["TenTaiKhoan"].ToString();
                    tk[i].MatMaTaiKhoan = dt.Rows[i]["MatMaTaiKhoan"].ToString();
                    tk[i].HoTenNhanVien = dt.Rows[i]["HoTenNhanVien"].ToString();
                    tk[i].MaQuyen = int.Parse(dt.Rows[i]["MaQuyen"].ToString());

                    tk[i].TenQuyen = dt.Rows[i]["TenQuyen"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return tk;
        }

        public TaiKhoan_dto GetTaiKhoan(string TenTaiKhoan)
        {
            List<SqlParameter> listpara = new List<SqlParameter>();
            listpara.Add(new SqlParameter("@TenTaiKhoan", TenTaiKhoan));

            DataTable dt = MyExecute.ExecuteQuery("sp_LayTaiKhoanTuongUng", listpara);
            if (dt.Rows.Count >= 1)
            {
                TaiKhoan_dto tk = new TaiKhoan_dto();
                tk.TenTaiKhoan = dt.Rows[0]["TenTaiKhoan"].ToString();
                tk.MatMaTaiKhoan = dt.Rows[0]["MatMaTaiKhoan"].ToString();
                tk.HoTenNhanVien = dt.Rows[0]["HoTenNhanVien"].ToString();
                tk.MaQuyen = int.Parse(dt.Rows[0]["MaQuyen"].ToString());

                tk.TenQuyen = dt.Rows[0]["TenQuyen"].ToString();
                return tk;
            }
            return null;
        }
    }

}
